<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Word extends Model
{
    protected $fillable = ['id', 'book_id', 'book_key', 'word_key', 'word_rank', 'word', 'phonetic_us', 'phonetic_uk',
        'speech_uk', 'speech_us', 'cover'];

    const POS = [
        'n' => '名词',
        'pron' => '代词',
        'adj' => '形容词',
        'adv' => '副词',
        'v' => '动词',
        'num' => '数词',
        'art' => '冠词',
        'prep' => '介词',
        'conj' => '连词',
        'interj' => '感叹词',
    ];

    public function book(): BelongsTo
    {
        return $this->belongsTo(Book::class);
    }

    public function trans(): HasMany
    {
        return $this->hasMany(WordTrans::class);
    }

    public function exams(): HasMany
    {
        return $this->hasMany(WordExam::class);
    }

    public function sentences(): HasMany
    {
        return $this->hasMany(WordSentence::class);
    }

    public function phrases(): HasMany
    {
        return $this->hasMany(WordPhrase::class);
    }

    public function scopeSort($query)
    {
        return $query->orderBy('word_rank');
    }

    public function speech($param)
    {
        return 'https://dict.youdao.com/dictvoice?audio='.$param;
    }
}
